//
//  ComplainDetailView.m
//  智慧小区
//
//  Created by lab603 on 15/7/6.
//  Copyright (c) 2015年 Dreamsoul. All rights reserved.
//

#import "ComplainDetailView.h"
#import "Complain.h"
#import "PPPicture.h"
#import "PPPicturesView.h"

#define padding 10
@interface ComplainDetailView()
//业主投诉信息背景
@property (nonatomic,weak)UIImageView *ownerBackground;
//投诉建议详细信息
@property (nonatomic,weak)UILabel *detailLabel;
//语音按钮
@property (nonatomic,weak)UIButton *voiceBtn;
//存放图片的View
@property (nonatomic,weak) PPPicturesView *picturesView;
//姓名
@property (nonatomic,weak)UILabel *nameLabel;
//日期
@property (nonatomic,weak)UILabel *ownerTimeLabel;

//物业回馈信息背景
@property (nonatomic,weak)UIImageView *propertyBackground;
//物业回馈信息
@property (nonatomic,weak)UILabel *propertyBackLabel;
//物业管理Label
@property (nonatomic,weak)UILabel *propertyManageLabel;
//时间Label
@property (nonatomic,weak)UILabel *propertyTimeLabel;

//评价背景
@property (nonatomic,weak)UIImageView *evaluateBackgound;
//提示Label
@property (nonatomic,weak)UILabel *tipLabel;
//提交按钮
@property (nonatomic,weak)UIButton *commitBtn;
//感动到流泪按钮
@property (nonatomic,weak)UIButton *goodBtn;
//一般按钮
@property (nonatomic,weak)UIButton *soSoBtn;
//差劲按钮
@property (nonatomic,weak)UIButton *badBtn;
@end
@implementation ComplainDetailView
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //业主投诉信息背景
        UIImageView *ownerBackground = [[UIImageView alloc] init];
        ownerBackground.backgroundColor = [UIColor whiteColor];
        ownerBackground.userInteractionEnabled = YES;
        [self addSubview:ownerBackground];
        _ownerBackground =ownerBackground;
        //投诉建议详细信息
        UILabel *detailLabel = [[UILabel alloc] init];
        detailLabel.font = [UIFont systemFontOfSize:15.0];
        detailLabel.textColor = [UIColor blackColor];
        detailLabel.backgroundColor = [UIColor clearColor];
        detailLabel.numberOfLines = 0;
        [self.ownerBackground addSubview:detailLabel];
        _detailLabel = detailLabel;
        //语音按钮
        UIButton *voiceBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [voiceBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [voiceBtn setTitle:@"播放语音" forState:UIControlStateNormal];
        [voiceBtn setBackgroundColor:APP_THEME_COLOR];
        voiceBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [voiceBtn.layer setCornerRadius:CORNER_RADIUS_5];
        voiceBtn.clipsToBounds = YES;
        [voiceBtn addTarget:self action:@selector(voicePlay) forControlEvents:UIControlEventTouchUpInside];
        [self.ownerBackground addSubview: voiceBtn];
        self.voiceBtn = voiceBtn;
        //存放图片的View
        PPPicturesView *picturesView = [[PPPicturesView alloc] init];
        [self.ownerBackground addSubview:picturesView];
        _picturesView = picturesView;
        //姓名
        UILabel *nameLabel = [[UILabel alloc] init];
        nameLabel.font = [UIFont systemFontOfSize:15.0];
        nameLabel.textColor = [UIColor blackColor];
        nameLabel.backgroundColor = [UIColor clearColor];
        [self.ownerBackground addSubview:nameLabel];
        _nameLabel = nameLabel;
        //时间Label
        UILabel *ownerTimeLabel = [[UILabel alloc] init];
        ownerTimeLabel.font = [UIFont systemFontOfSize:15.0];
        ownerTimeLabel.textColor = [UIColor blackColor];
        ownerTimeLabel.backgroundColor = [UIColor clearColor];
        [self.ownerBackground addSubview:ownerTimeLabel];
        _ownerTimeLabel = ownerTimeLabel;
        
        //物业回馈背景
        UIImageView *propertyBackground = [[UIImageView alloc] init];
        propertyBackground.backgroundColor = [UIColor whiteColor];
        propertyBackground.userInteractionEnabled = YES;
        [self addSubview:propertyBackground];
        _propertyBackground =propertyBackground;
        //物业回馈信息
        UILabel *propertyBackLabel = [[UILabel alloc] init];
        propertyBackLabel.font = [UIFont systemFontOfSize:15.0];
        propertyBackLabel.textColor = [UIColor blackColor];
        propertyBackLabel.backgroundColor = [UIColor clearColor];
        [self.propertyBackground addSubview:propertyBackLabel];
        _propertyBackLabel = propertyBackLabel;
        //物业管理Label
        UILabel *propertyManageLabel = [[UILabel alloc] init];
        propertyManageLabel.font = [UIFont systemFontOfSize:15.0];
        propertyManageLabel.textColor = [UIColor blackColor];
        propertyManageLabel.backgroundColor = [UIColor clearColor];
        [self.propertyBackground addSubview:propertyManageLabel];
        _propertyManageLabel = propertyManageLabel;
        //回馈时间Label
        UILabel *propertyTimeLabel = [[UILabel alloc] init];
        propertyTimeLabel.font = [UIFont systemFontOfSize:15.0];
        propertyTimeLabel.textColor = [UIColor blackColor];
        propertyTimeLabel.backgroundColor = [UIColor clearColor];
        [self.propertyBackground addSubview:propertyTimeLabel];
        _propertyTimeLabel = propertyTimeLabel;
        
        //评价背景图
        UIImageView *evaluateBackgound = [[UIImageView alloc] init];
        evaluateBackgound.backgroundColor = [UIColor whiteColor];
        evaluateBackgound.userInteractionEnabled = YES;
        [self addSubview:evaluateBackgound];
        _evaluateBackgound =evaluateBackgound;
        //提交按钮
        UIButton *commitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [commitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [commitBtn setTitle:@"提交" forState:UIControlStateNormal];
        [commitBtn setBackgroundColor:APP_THEME_COLOR];
        commitBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [commitBtn.layer setCornerRadius:CORNER_RADIUS_5];
        commitBtn.clipsToBounds = YES;
        [commitBtn addTarget:self action:@selector(commitEvaluate) forControlEvents:UIControlEventTouchUpInside];
        [self.evaluateBackgound addSubview: commitBtn];
        _commitBtn = commitBtn;
        //感动到流泪按钮
        UIButton *goodBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [goodBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [goodBtn setTitle:@"感动到流泪" forState:UIControlStateNormal];
        goodBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [goodBtn addTarget:self action:@selector(chooseEvaluate) forControlEvents:UIControlEventTouchUpInside];
        [self.evaluateBackgound addSubview: goodBtn];
        _goodBtn = goodBtn;
        //一般按钮
        UIButton *soSoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [soSoBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [soSoBtn setTitle:@"一般(还算满意)" forState:UIControlStateNormal];
        soSoBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [soSoBtn addTarget:self action:@selector(chooseEvaluate) forControlEvents:UIControlEventTouchUpInside];
        [self.evaluateBackgound addSubview: soSoBtn];
        _soSoBtn = soSoBtn;
        //差劲按钮
        UIButton *badBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [badBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [badBtn setTitle:@"处理地有点差劲" forState:UIControlStateNormal];
        badBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [badBtn addTarget:self action:@selector(chooseEvaluate) forControlEvents:UIControlEventTouchUpInside];
        [self.evaluateBackgound addSubview: badBtn];
        _badBtn = badBtn;
    }
    return self;
}
- (void)setComplain:(Complain *)complain{
    _complain = complain;
    //设置控件的frame
    //业主投诉背景Frame
    CGFloat ownerBackgroundX = 0;
    CGFloat ownerBackgroundY = padding;
    CGFloat ownerBackgroundW = ScreenWidth;
    //业主投诉背景高度待定
    CGFloat ownerBackgroundH = 0;
    //投诉建议详细信息Frame
    CGFloat detailLabelX = padding*2;
    CGFloat detailLabelY = padding*2;
    CGSize detailLabelSize = [complain.texts sizeWithFont:[UIFont systemFontOfSize:15.0] constrainedToSize:(CGSize){ScreenWidth-4*padding,MAXFLOAT}];
    _detailLabel.frame = (CGRect){{detailLabelX,detailLabelY},detailLabelSize};
    //语音播放按钮
    CGFloat voiceBtnX = detailLabelX;
    CGFloat voiceBtnY = CGRectGetMaxY(_detailLabel.frame)+2*padding;
    CGSize voiceBtnSize = (CGSize){70,30};
    _voiceBtn.frame = (CGRect){{voiceBtnX,voiceBtnY},voiceBtnSize};
    //存放图片的View
    CGFloat picturesViewX = voiceBtnX;
    CGFloat picturesViewY = CGRectGetMaxY(self.voiceBtn.frame)+padding;
    if (complain.voice==nil) {
        self.voiceBtn.hidden = YES;
        picturesViewY = CGRectGetMaxY(self.detailLabel.frame)+padding;
    }
    NSMutableArray *picturesArray = [[NSMutableArray alloc] init];
    int picturesCount = 0;
    if (complain.pictureThree) {
        PPPicture *picture1 = [[PPPicture alloc] init];
        NSString *pictureOneUrl = [StaticFileUrl stringByAppendingString:complain.pictureOne];
        picture1.picture = pictureOneUrl;
        [picturesArray addObject:picture1];
        
        PPPicture *picture2 = [[PPPicture alloc] init];
        NSString *pictureTwoUrl = [StaticFileUrl stringByAppendingString:complain.pictureTwo];
        picture2.picture = pictureTwoUrl;
        [picturesArray addObject:picture2];
        
        PPPicture *picture3 = [[PPPicture alloc] init];
        NSString *pictureThreeUrl = [StaticFileUrl stringByAppendingString:complain.pictureThree];
        picture3.picture = pictureThreeUrl;
        [picturesArray addObject:picture3];
        picturesCount=3;
    }else if(complain.pictureTwo){
        PPPicture *picture1 = [[PPPicture alloc] init];
        NSString *pictureOneUrl = [StaticFileUrl stringByAppendingString:complain.pictureOne];
        picture1.picture = pictureOneUrl;
        [picturesArray addObject:picture1];
        
        PPPicture *picture2 = [[PPPicture alloc] init];
        NSString *pictureTwoUrl = [StaticFileUrl stringByAppendingString:complain.pictureTwo];
        picture2.picture = pictureTwoUrl;
        [picturesArray addObject:picture2];
        picturesCount = 2;
    }else if(complain.pictureOne){
        PPPicture *picture1 = [[PPPicture alloc] init];
        NSString *pictureOneUrl = [StaticFileUrl stringByAppendingString:complain.pictureOne];
        picture1.picture = pictureOneUrl;
        [picturesArray addObject:picture1];
        picturesCount = 1;
    }else{
        self.picturesView.hidden = YES;
    }
    CGSize picturesViewSize = [PPPicturesView picturesViewCount:picturesCount];
    _picturesView.frame = (CGRect){{picturesViewX,picturesViewY},picturesViewSize};
    //姓名label
    CGFloat nameLabelX = padding*2;
    CGFloat nameLabelY = 0;
    if(complain.pictureOne==nil&&complain.voice==nil){
        nameLabelY = CGRectGetMaxY(_detailLabel.frame)+padding;
    }else if(complain.pictureOne==nil&&complain.voice!=nil){
        nameLabelY = CGRectGetMaxY(_voiceBtn.frame)+padding;
    }else if(complain.pictureOne!=nil&&complain.voice==nil){
        nameLabelY = CGRectGetMaxY(_picturesView.frame)+padding;
    }else{
        nameLabelY = CGRectGetMaxY(_picturesView.frame)+padding;
    }
    CGSize nameLabelSize = [complain.name sizeWithFont:[UIFont systemFontOfSize:15.0]];
    _nameLabel.frame = (CGRect){{nameLabelX,nameLabelY},nameLabelSize};
    //业主时间label
    CGSize ownerTimeLabelSize = [complain.lastEditTime sizeWithFont:[UIFont systemFontOfSize:15.0]];
    CGFloat ownerTimeLabelX = ScreenWidth-2*padding-ownerTimeLabelSize.width;
    CGFloat ownerTimeLabelY = nameLabelY;
    _ownerTimeLabel.frame = (CGRect){{ownerTimeLabelX,ownerTimeLabelY},ownerTimeLabelSize};
    ownerBackgroundH = CGRectGetMaxY(_ownerTimeLabel.frame)+padding;
    _ownerBackground.frame = CGRectMake(ownerBackgroundX, ownerBackgroundY, ownerBackgroundW, ownerBackgroundH);
    
    //物业回馈信息背景Frame
    CGFloat propertyBackgroundX = ownerBackgroundX;
    CGFloat propertyBackgroundY = CGRectGetMaxY(_ownerBackground.frame)+padding;
    CGFloat propertyBackgroundW = ScreenWidth;
    //物业回馈信息背景暂定
    CGFloat propertyBackgroundH = 0;
    NSString *commentString = @"";
    if (complain.comment==nil) {
        commentString = @"无";
    }else{
        commentString = complain.comment;
    }
    //物业回馈信息Frame
    CGFloat propertyBackLabelX = 2*padding;
    CGFloat propertyBackLabelY = 2*padding;
    CGSize propertyBackLabelSize = [commentString sizeWithFont:[UIFont systemFontOfSize:15.0] constrainedToSize:(CGSize){ScreenWidth-2*padding,MAXFLOAT}];
    _propertyBackLabel.frame = (CGRect){{propertyBackLabelX,propertyBackLabelY},propertyBackLabelSize};
    //物业管理Label
    CGFloat propertyManageLabelX = propertyBackLabelX;
    CGFloat propertyManageLabelY = CGRectGetMaxY(_propertyBackLabel.frame)+2*padding;
    CGSize propertyManageLabelSize = [@"物业管理" sizeWithFont:[UIFont systemFontOfSize:15.0]];
    _propertyManageLabel.frame = (CGRect){{propertyManageLabelX,propertyManageLabelY},propertyManageLabelSize};
    //物业反馈时间
    CGSize propertyTimeLabelSize = [complain.lastEditTime sizeWithFont:[UIFont systemFontOfSize:15.0]];
    CGFloat propertyTimeLabelX = ScreenWidth - 2*padding - propertyTimeLabelSize.width;
    CGFloat propertyTimeLabelY = propertyManageLabelY;
    _propertyTimeLabel.frame = (CGRect){{propertyTimeLabelX,propertyTimeLabelY},propertyTimeLabelSize};
    propertyBackgroundH = CGRectGetMaxY(_propertyTimeLabel.frame)+padding;
    _propertyBackground.frame = CGRectMake(propertyBackgroundX, propertyBackgroundY, propertyBackgroundW, propertyBackgroundH);
    if (complain.status==0) {
        self.propertyBackground.hidden = YES;
        _viewHeight = CGRectGetMaxY(_ownerBackground.frame);
    }else if(complain.status==1){
        self.propertyBackground.hidden = NO;
        _viewHeight = CGRectGetMaxY(_propertyBackground.frame);
    }
    //为控件赋值
    self.detailLabel.text = complain.texts;
    self.nameLabel.text = complain.name;
    self.ownerTimeLabel.text = complain.lastEditTime;
    self.picturesView.picturesArray = picturesArray;
    self.propertyBackLabel.text = commentString;
    self.propertyManageLabel.text = @"物业管理";
    self.propertyTimeLabel.text = complain.lastEditTime;
}
/**
 *  选择评价按钮被点击
 */
- (void)chooseEvaluate{
    
}
/**
 *  提交按钮被点击
 */
- (void)commitEvaluate{
    
}
/**
 *  语音按钮被点击
 */
- (void)voicePlay{
    IWLog(@"播放语音");
    if (self.tapVoiceBtnBlock) {
        self.tapVoiceBtnBlock();
    }
}
@end
